#include "MainWindow.h"
#include <QResizeEvent>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QList>
#include <QStringList>
#include <QFileDialog>


MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    resize(720,520);
    setWindowTitle("Shape Editor");
    setFixedSize(720,520);
    rectangleButton = new QPushButton("Rectangle");
    rectangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
    triangleButton = new QPushButton("Triangle");
    triangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
    circleButton = new QPushButton("Circle");
    circleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");

    //removeShapeButton = new QPushButton("Remove Figure");
    //removeShapeButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
    clearButton = new QPushButton("Clear Scene");
    clearButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");

    text = new QTextEdit;
    text->setFixedSize(200,200);

    a = new QTextEdit;
    a->setFixedSize(30,25);
    a->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    a->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    b = new QTextEdit;
    b->setFixedSize(30,25);
    b->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    b->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QHBoxLayout *datalayout = new QHBoxLayout;
    datalayout->addWidget(b);
    datalayout->addWidget(a);

    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addWidget(rectangleButton);
    vlayout->addWidget(triangleButton);
    vlayout->addWidget(circleButton);
    vlayout->addWidget(text);
    vlayout->addLayout(datalayout);
    //vlayout->addWidget(removeShapeButton);
    vlayout->addWidget(clearButton);

    graphicsView = new QGraphicsView();
    graphicsView->resize(500, 450);
    QHBoxLayout *hlayout = new QHBoxLayout;
    hlayout->addWidget(graphicsView);
    hlayout->addLayout(vlayout);
    setLayout(hlayout);

    graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scene = new QGraphicsScene();
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    scene->setSceneRect(10,10,500,500);
    graphicsView->setScene(scene);
    graphicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    connect(rectangleButton, SIGNAL(clicked(bool)), SLOT(RectangleSetButtonEnabled()));
    connect(triangleButton, SIGNAL(clicked(bool)), SLOT(TriangleSetButtonEnabled()));
    connect(circleButton, SIGNAL(clicked(bool)), SLOT(CircleSetButtonEnabled()));
    //connect(removeShapeButton, SIGNAL(clicked(bool)),SLOT(RemoveFigure()));
    connect(clearButton, SIGNAL(clicked(bool)),this, SLOT(ClearScene()));

    text->insertPlainText("1.Choose what you want to draw\n");
    text->insertPlainText("2.Print size of the sides under\n");
    text->insertPlainText("3.Then double clicked on windows\n");
    text->insertPlainText("-------------------------------------------\n");
    text->insertPlainText("To move the shape press left mouse button and move shape holding it\n\n");
    text->insertPlainText("To rotate the shape on 10 degree select it (press right mouse button) and press Space\n");
    text->insertPlainText("-------------------------------------------\n\n");

    numberOfShape = 0;
//    loadingInf();
}

MainWindow::~MainWindow()
{
    msgBox.setWindowTitle("Savings");
    msgBox.setIcon(QMessageBox::Question);
    msgBox.setText("Do you want to save settings of shape conteiner?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::Yes);

    int ret = 0/*msgBox.exec()*/;
    switch (ret) {
    case QMessageBox::Yes:
    {
        int sizeOfConteiner = conteinerOfShape.size();
        for(int i=0; i < sizeOfConteiner; i++)
        {
            conteinerOfShape.RemoveFig(0);
        }
        for(int i=0; i<listOfItem.size(); i++)
        {

            if(listOfItem[i]->typeItem == "r")
            {
                Rectangle *r = new Rectangle(listOfItem[i]->coord_x, listOfItem[i]->coord_y, listOfItem[i]->a, listOfItem[i]->b, listOfItem[i]->angel);
                conteinerOfShape.InsertFig(r);
            }
            if(listOfItem[i]->typeItem == "t")
            {
                 Triangle *t = new Triangle(listOfItem[i]->coord_x, listOfItem[i]->coord_y, listOfItem[i]->a, listOfItem[i]->b,listOfItem[i]->angel);
                 conteinerOfShape.InsertFig(t);
            }
            if(listOfItem[i]->typeItem == "c")
            {
                Circle *c = new Circle(listOfItem[i]->coord_x, listOfItem[i]->coord_y, listOfItem[i]->a,listOfItem[i]->angel);
                conteinerOfShape.InsertFig(c);
            }
        }
        QString strFile = QFileDialog::getSaveFileName(this,"Save File");

        QFile fileOut(strFile);
        if(conteinerOfShape.size() == 0)
        {
            fileOut.resize(0);
            break;
        }
        fileOut.resize(0);
        conteinerOfShape.writeToFile(strFile);
        break;
    }
    default:
    {
        break;
    }
    }
    delete graphicsView;
    delete rectangleButton;
    delete triangleButton;
    delete circleButton;
    delete clearButton;
    delete text;
    delete a;
    delete b;
    for(int i=0; i<listOfItem.size(); i++)
    {
       delete listOfItem[i];
    }
    delete scene;

}

void MainWindow::loadingInf()
{
    msgBoxLoad.setWindowTitle("Loadings");
    msgBoxLoad.setIcon(QMessageBox::Question);
    msgBoxLoad.setText("Do you want to load settings of shape conteiner?");
    msgBoxLoad.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBoxLoad.setDefaultButton(QMessageBox::Yes);

    msgBoxLoad.show();
    int ret = msgBoxLoad.exec();

    switch (ret) {
    case QMessageBox::Yes:
    {
        QString strFile = QFileDialog::getOpenFileName(this,"Open File");
        conteinerOfShape.readFromFile(strFile);
        for(int i=0; i<conteinerOfShape.size(); i++)
        {
            shapeItem *item = new shapeItem();
            if(conteinerOfShape[i]->Print_type() == "CIRCLE")
            {
                item->typeItem = "c";
            }
            if(conteinerOfShape[i]->Print_type() == "TRIANGLE")
            {
                item->typeItem = "t";
            }
            if(conteinerOfShape[i]->Print_type() == "RECTANGLE")
            {
                item->typeItem = "r";
            }
            PaintFromFile(item, conteinerOfShape[i]);
        }
        break;
    }
    default:
        break;
    }
}

void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    x = event->x();
    y = event->y();
    createItem();
}

void MainWindow::createItem()
{
    if (circleButton->isEnabled() && rectangleButton->isEnabled() && triangleButton->isEnabled())
    {
        text->clear();
        text->insertPlainText("1.Choose what you want to draw\n");
        text->insertPlainText("2.Print size of the sides under\n");
        text->insertPlainText("3.Then double clicked on windows\n");
        text->insertPlainText("-------------------------------------------\n");
        text->insertPlainText("To move the shape press left mouse button and move shape holding it\n\n");
        text->insertPlainText("To rotate the shape on 10 degree select it (press right mouse button) and press Space\n");
        text->insertPlainText("-------------------------------------------\n\n");
        text->insertPlainText("You didn't choose the Figure\n");
    }
    else
    {
        text->clear();
        text->insertPlainText("1.Choose what you want to draw\n");
        text->insertPlainText("2.Print size of the sides under\n");
        text->insertPlainText("3.Then double clicked on windows\n");
        text->insertPlainText("-------------------------------------------\n");
        text->insertPlainText("To move the shape press left mouse button and move shape holding it\n\n");
        text->insertPlainText("To rotate the shape on 10 degree select it (press right mouse button) and press Space\n");
        text->insertPlainText("-------------------------------------------\n\n");
        shapeItem *item = new shapeItem();

        if (rectangleButton->isEnabled())
        {
            item->index = numberOfShape;
            item->setPos(x,y);
            item->coord_x = x;
            item->coord_y = y;
            item->typeItem = "r";
        }
        else if (triangleButton->isEnabled())
        {
            item->index = numberOfShape;
            item->setPos(x,y);
            item->coord_x = x;
            item->coord_y = y;
            item->typeItem = "t";
        }
        else if (circleButton->isEnabled())
        {
            item->index = numberOfShape;
            item->setPos(x,y);
            item->coord_x = x;
            item->coord_y = y;
            item->typeItem = "c";
        }
        ReadData(item);
        numberOfShape++;
    }
}

void MainWindow::PaintFromFile(shapeItem *item, Shape *fig)
{
    QList<QString>inf;
    inf = fig->getInf();
    if(item->typeItem == "c")
    {
        item->index = numberOfShape;
        item->setPos(inf[0].toDouble(), inf[1].toDouble());
        item->coord_x = inf[0].toDouble();
        item->coord_y = inf[1].toDouble();
        item->a = inf[2].toDouble();
        item->b = item->a;
        item->angel = inf[3].toDouble();

    }
    if(item->typeItem == "r")
    {
        item->index = numberOfShape;
        item->setPos(inf[0].toDouble(), inf[1].toDouble());
        item->coord_x = inf[0].toDouble();
        item->coord_y = inf[1].toDouble();
        item->a = inf[2].toDouble();
        item->b = inf[3].toDouble();
        item->angel = inf[4].toDouble();
    }
    if(item->typeItem == "t")
    {
        item->index = numberOfShape;
        item->setPos(inf[0].toDouble(), inf[1].toDouble());
        item->coord_x = inf[0].toDouble();
        item->coord_y = inf[1].toDouble();
        item->a = inf[2].toDouble();
        item->b = inf[3].toDouble();
        item->angel = inf[4].toDouble();
    }

    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    item->setFlag(QGraphicsItem::ItemIsFocusable,true);
    scene->addItem(item);
    item->setRotation(item->angel);
    listOfItem.insert(item->index,item);
}

void MainWindow:: ReadData(shapeItem *item)
{
        if (item->typeItem == "r")
        {
            item->a = a->toPlainText().toDouble();
            item->b = b->toPlainText().toDouble();
            item->angel = 0;
            a->clear();
            b->clear();
            circleButton->setEnabled(true);
            triangleButton->setEnabled(true);
            rectangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        }
        else if (item->typeItem == "t")
        {
            item->a = a->toPlainText().toDouble();
            item->b = b->toPlainText().toDouble();
            item->angel = 0;
            a->clear();
            b->clear();
            circleButton->setEnabled(true);
            rectangleButton->setEnabled(true);
            triangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        }
        else if (item->typeItem == "c")
        {
            item->a = b->toPlainText().toDouble();
            item->b = item->a;
            item->angel = 0;
            b->clear();
            a->setEnabled(true);
            triangleButton->setEnabled(true);
            rectangleButton->setEnabled(true);
            circleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        }

        item->setFlag(QGraphicsItem::ItemIsMovable, true);
        item->setFlag(QGraphicsItem::ItemIsSelectable, true);
        item->setFlag(QGraphicsItem::ItemIsFocusable,true);

        scene->addItem(item);
        listOfItem.insert(item->index,item);
}

void MainWindow::RectangleSetButtonEnabled()
{
    if(!(triangleButton->isEnabled()) && !(triangleButton->isEnabled()))
    {
        rectangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        triangleButton->setEnabled(true);
        circleButton->setEnabled(true);
    }
    else
    {
        rectangleButton->setStyleSheet("color: #F4FF23;" "background-color: #4356FF");
        triangleButton->setEnabled(false);
        circleButton->setEnabled(false);
     }
}

void MainWindow::TriangleSetButtonEnabled()
{
    if(!(rectangleButton->isEnabled()) && !(circleButton->isEnabled()))
    {
        triangleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        rectangleButton->setEnabled(true);
        circleButton->setEnabled(true);
    }
    else
    {
        triangleButton->setStyleSheet("color: #F4FF23;" "background-color: #4356FF");
        rectangleButton->setEnabled(false);
        circleButton->setEnabled(false);
    }
}

void MainWindow::CircleSetButtonEnabled()
{
    if(!(rectangleButton->isEnabled()) && !(triangleButton->isEnabled()))
    {
        circleButton->setStyleSheet("color: #000000;" "background-color: #E0E0E0");
        triangleButton->setEnabled(true);
        rectangleButton->setEnabled(true);
        a->setEnabled(true);
    }
    else
    {
        circleButton->setStyleSheet("color: #F4FF23;" "background-color: #4356FF");
        triangleButton->setEnabled(false);
        rectangleButton->setEnabled(false);
        a->setEnabled(false);
    }
}

void MainWindow::ClearScene()
{
    int sizeOfList = listOfItem.size();
    for(int i=0; i<sizeOfList; i++)
    {
        listOfItem.removeFirst();
    }
    scene->clear();
    numberOfShape=0;
}

/*void MainWindow::RemoveFigure()
{
    QList<QGraphicsItem*> selectedItems = scene->selectedItems();
        if (selectedItems.size()!=0)
        {
            QList<shapeItem*>selected;
            int sizeOfSelected = selectedItems.size();
            for(int i=0; i<sizeOfSelected; i++)
            {
                if(listOfItem[i]->isSelected())
                {
                    selected.removeAll(listOfItem[i]);
                }
            }

            for (int i=0; i<sizeOfSelected; i++)
            {
                scene->removeItem(selectedItems[i]);
            }
        }
}*/

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    QList<QGraphicsItem*> selectedItems = scene->selectedItems();
    if (selectedItems.size()!=0)
    {
        if(event->key() == Qt::Key_Space )
        {
            for (int i=0; i<selectedItems.size(); i++)
            {
                selectedItems[i]->setRotation((selectedItems[i]->rotation())+10);
            }
        }
    }
}

